package com.ruoyi.jihuo.api.official;

import com.ruoyi.jihuo.api.ApiStrategy;
import com.ruoyi.jihuo.domain.JhTmAccount;
import com.taobao.top.BaseTopApiRequest;
import com.taobao.top.BaseTopApiResponse;
import com.taobao.top.TopApiClient;
import com.taobao.top.TopFileItem;
import com.taobao.top.defaultability.Defaultability;
import com.taobao.top.defaultability.request.TaobaoPictureUploadRequest;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Map;

@Component
public class PictureUploadStrategy implements ApiStrategy {
    @Override
    public BaseTopApiResponse execute(TopApiClient client, BaseTopApiRequest request, JhTmAccount jhTmAccount) throws IOException {
        Defaultability apiPackage = new Defaultability(client);
        return apiPackage.taobaoPictureUpload((TaobaoPictureUploadRequest) request, jhTmAccount.getJhAppSession());
    }

    @Override
    public BaseTopApiRequest setParams(String api, Map<String, Object> paramMap) {
        TaobaoPictureUploadRequest request = new TaobaoPictureUploadRequest();
        request.setPictureCategoryId((long) paramMap.get("picture_category_id"));

        try {
            byte[] bytes = Files.readAllBytes(new File((String) paramMap.get("file_path")).toPath());
            request.setImg(new TopFileItem("2333.jpg", bytes));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        request.setImageInputTitle("2333");
        return request;
    }
}
